package com.etc.dao.impl;

import java.util.List;

import com.etc.dao.UserDao;
import com.etc.entity.User;
import com.etc.util.DBUtil;
import com.etc.util.PageData;

/**
 * @author Administrator shift+alt+j
 *
 */
public class UserDaoImpl implements UserDao {

	@Override
	public User userLogin(String username, String userpwd) {
		String sql = "select * from tbl_user where username=? and userpwd=? and userstate=1";
		List<User> list = (List<User>) DBUtil.exQuery(sql, User.class, username, userpwd);
		// list里有记录，登录成功
		if (list.size() > 0) {
			return list.get(0);
		}
		// 登录失败
		return null;
	}

	/**
	 * 新增用户
	 * 
	 * @param user User对象
	 * @return true操作成功 false 操作失败
	 */
	@Override
	public boolean addUser(User user) {
		String sql = "insert into tbl_user values(null,?,?,?,?,?)";
		return DBUtil.exUpdate(sql, user.getUsername(), user.getUserpwd(), user.getUserphone(), user.getUserstate(),
				user.getAddress()) > 0;
	}

	@Override
	public boolean updateUser(int userid, int userstate) {
		String sql = "update tbl_user set  userstate=? where userid=?";
		// TODO Auto-generated method stub
		return DBUtil.exUpdate(sql, userstate, userid) > 0;
	}

	/**
	 *
	 */
	@Override
	public boolean modUser(int uid, String username, String userpwd, String userphone, String address) {
		String sql = "update tbl_user set username=?,userpwd=?,userphone=?,address=? where userid=?";
		return DBUtil.exUpdate(sql, username, userpwd, userphone, address, uid) > 0;

	}

	/**
	 * 
	 */

	@Override
	public User getUserByName(String username) {
		// TODO Auto-generated method stub
		String sql = "select * from tbl_user where username = ?";
		List<User> list = (List<User>) DBUtil.exQuery(sql, User.class, username);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<User> getUser(String keywords) {
		// TODO Auto-generated method stub
		String sql = "select * from tbl_user where username like ?";
		List<User> list = (List<User>) DBUtil.exQuery(sql, User.class, "%" + keywords + "%");
		return list;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: getUserByPage
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param keywords
	 * @return
	 * 
	 * @see com.etc.zmall.dao.UserDao#getUserByPage(int, int, java.lang.String)
	 * 
	 */
	@Override
	public PageData<User> getUserByPage(int pageNo, int pageSize, String keywords, String usersex) {
		// sql不固定，我们需要判断(二者都为空，按照用户名关键字查询)
		PageData<User> pd = null;
		String sql = "select * from tbl_user where username like ? ";
		// 用户名传递关键字 性别为空
		if (!("".equals(keywords)) && !("".equals(usersex))) {

			sql = "select * from tbl_user where username like ? and usersex = ? ";
			pd = DBUtil.exQueryByPage(sql, User.class, pageNo, pageSize, "%" + keywords + "%", usersex);
		}
		// 用户名关键字为空 性别不为空
		else if (!("".equals(usersex)) && ("".equals(keywords))) {
			sql = "select * from tbl_user where usersex = ? ";
			pd = DBUtil.exQueryByPage(sql, User.class, pageNo, pageSize, usersex);
		} else {
			pd = DBUtil.exQueryByPage(sql, User.class, pageNo, pageSize, "%" + keywords + "%");

		}
		return pd;
	}

	@Override
	public User getUserById(int userid) {
		String sql = "select * from tbl_user where userid = ?";
		List<User> list = DBUtil.exQuery(sql, User.class, userid);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

}
